package it.mauriziobasile.sincrodir.logica;

import it.mauriziobasile.sincrodir.entita.Directory;
import it.mauriziobasile.sincrodir.entita.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileSystem {
	
	public static void copia(String sorgente,String destinazione) throws IOException{
		copia(new File(sorgente), new File(destinazione));
	}

	public static void copia(File sorgente,File destinazione) throws IOException{
		FileChannel inChannel = new FileInputStream(sorgente).getChannel();
		FileChannel outChannel = new FileOutputStream(destinazione).getChannel();
		try {
			inChannel.transferTo(0, inChannel.size(),outChannel);
		} 
		catch (IOException e) {
			throw e;
		}
		finally {
	        if (inChannel != null) inChannel.close();
	        if (outChannel != null) outChannel.close();
		}	
	}

	public static boolean creaDirectory(Directory dir){
		return dir.mkdirs();
	}
	
	public static boolean esisteDir(String dire){
	    Directory dir = new Directory(dire,false);
	    boolean ex=false;
	    try{
	        ex=dir.exists();
	    }
	    catch(SecurityException se){
	        ex=false;
	    }
	    return ex;
	}
}
